﻿///<reference path="../../ExtJs/vswd-ext_2.2.js" />

var client_id;
var pageSize = 15;  // Grid每页显示个数
var cellWidth = 100; //单元格宽度

Ext.onReady(function() {
    Ext.QuickTips.init();
    // 本地加载图片代码
    Ext.BLANK_IMAGE_URL = strPath + 'extjs/resources/images/default/s.gif';

    pageSize = parseInt(((parent.Ext.get("ContentFrame").getHeight() - 108) - 1) / 21); //根据页面高度显示记录个数

    cellWidth = parseInt((parent.Ext.get("ContentFrame").getWidth() - 80 - 24));
    var httpProxy;

    // 设置获取订单列表的地址
    var httpProxyClient_Main = new Ext.data.HttpProxy({
        url: strPath + 'GGFW/DocumentsManage/ClientCommand.fwf?Method=GetClientList'
    });

    var readerProxyClient_Main = new Ext.data.JsonReader({
        root: 'Records',
        id: 'HWTransferItemID',
        totalProperty: 'totalCount',
        fields: [
          { name: 'client_id' }
          , { name: 'client_code' }
          , { name: 'client_name' }
          , { name: 'maokuan' }
          , { name: 'maitou' }
          , { name: 'maitou_image' }
          , { name: 'diaopai' }
          , { name: 'diaopai_image' }
        ]
    });

    // 定义数据源
    Client_MainStore = new Ext.data.Store({
        proxy: httpProxyClient_Main,
        reader: readerProxyClient_Main,
        remoteSort: true
    });

    // 订单grid的选择列的事件
    var smClientListModel = new Ext.grid.CheckboxSelectionModel({
        singleSelect: false,
        listeners: {
            rowselect: function(sm, row, rec) {
                client_id = rec.get("client_id");
            }
        }
    });

    // 设置显示列的Title
    var ChooseView = new Ext.grid.GridView({ forceFit: true, sortAscText: '升序', sortDescText: '降序' });
    ChooseView.columnsText = '列显示/隐藏';

    // 设置页脚的显示样式。
    var ChooseBar = new Ext.PagingToolbar({
        pageSize: pageSize,
        store: Client_MainStore,
        beforePageText: '第',
        afterPageText: '页 共{0}页',
        firstText: '第一页',
        prevText: '前一页',
        nextText: '后一页',
        lastText: '最后一页',
        refreshText: '刷新',
        displayInfo: true,
        displayMsg: '显示第 {0} - {1} 条数据, 共 {2} 条数据',
        emptyMsg: "没有数据可以显示"
    });

    // Grid显示内容
    var Client_MainlistColumnModel = new Ext.grid.ColumnModel([
        smClientListModel,
        { id: 'client_id', header: "client_id", align: 'center', sortable: false, dataIndex: 'client_id', hidden: true },
        { header: "编号", align: 'center', sortable: true, dataIndex: 'client_code', renderer: renderValue, width: cellWidth * 0.2 },
		{ header: "客户名", align: 'center', sortable: true, dataIndex: 'client_name', renderer: renderValue, width: cellWidth * 0.35 },
		{ header: "帽款", align: 'center', sortable: false, dataIndex: 'maokuan', renderer: renderValue, width: cellWidth * 0.15 },
		{ header: "唛头", align: 'center', sortable: false, dataIndex: 'maitou', renderer: renderValue, width: cellWidth * 0.15 },
		{ header: "吊牌", align: 'center', sortable: false, dataIndex: 'diaopai', renderer: renderValue, width: cellWidth * 0.15 },
		{ header: "操作", align: 'center', sortable: false, dataIndex: 'client_id', width: 80, renderer: getitemlink }
     ]);

    /**
    *描述 : 渲染列，鼠标移该单元格上面，显示完整的值
    *参数 : value：单元格的值；
    */
    function renderValue(value, metadate, record, colIndex, rowIndex) {
        return '<div style="text-align:left;"><span title="' + (value == null || value == "" ? "" : value) + '">' + (value == null || value == "" ? "" : value) + '</span></div>';
    }

    // 删除按钮
    var buttonDelClient = {
        id: 'btnDelClient',
        text: '<font color="#000000">删除</font>',
        handler: function() {
            DelClient();  // 删除调用的函数
        },
        iconCls: 'add',
        icon: strPath + 'images/ico_25.gif'

    };

    var labelSearchClientCode = new Ext.form.Label({
        id: "lbSearchClientCode",
        html: "<span>编号：</span>"
    });

    var textfieldSearchClientCode = new Ext.form.TextField({
        id: "tfSearchClientCode",
        width: 128
    });

    var labelSearchClientName = new Ext.form.Label({
        id: "lbSearchClientName",
        html: "<span>&nbsp;&nbsp;客户名：</span>"
    });

    var textfieldSearchClientName = new Ext.form.TextField({
        id: "tfSearchClientName",
        width: 128
    });

    // 工具条
    var toolbarClientList = new Ext.Toolbar({
        height: 25,
        items: [buttonDelClient, ' ', '-', ' ', labelSearchClientCode, textfieldSearchClientCode, labelSearchClientName, textfieldSearchClientName]
    });

    gridClientList = new Ext.grid.EditorGridPanel({
        id: "Client_MainGrid",
        renderTo: "divClient_Main",
        stripeRows: true,
        store: Client_MainStore,
        cm: Client_MainlistColumnModel,
        sm: smClientListModel,
        width: parent.Ext.get("ContentFrame").getWidth(),
        height: parent.Ext.get("ContentFrame").getHeight() - 25,
        loadMask: { msg: '正在加载数据，请稍侯... ...' },
        frame: false,
        border: false,
        animCollapse: false,
        trackMouseOver: true,
        bbar: ChooseBar,
        tbar: toolbarClientList
    });

    //数据源开始读取数据
    doSearchClientList(true);
})

// 加载数据(刷新)
function doSearchClientList(isFirstPage) {
    if (Client_MainStore == null) return;
    // 传递查询参数并刷新
    if (isFirstPage) Client_MainStore.load({
        params: {
            start: 0, // 起始页
            limit: pageSize, // 每页显示个数
            client_code: Ext.get("tfSearchClientCode").dom.value,
            client_name: Ext.get("tfSearchClientName").dom.value
        }
    });
    else Client_MainStore.load({
        params: {
            start: (gridClientList.getBottomToolbar().getPageData().activePage - 1) * pageSize,  // 起始页
            limit: pageSize,  // 每页显示个数
            client_code: Ext.get("tfSearchClientCode").dom.value,
            client_name: Ext.get("tfSearchClientName").dom.value
        }
    });
}

//页面跳转
function getitemlink(value, metadate, record, colIndex, rowIndex) {
    return "<a class='a_link' href=\"ClientEdit.aspx?client_id=" + record.get("client_id") + "\">编辑</a>";
}

// 删除方法
function DelClient() {
    var client_ids = getSelectID(gridClientList, 'client_id');
    if (client_ids == '' || client_ids == null) {
        Ext.MessageBox.alert("提示", "请选择要删除的数据！"); return;
    }
    Ext.MessageBox.confirm("询问", '确认要删除选中的数据吗？', function(btn) {
        if (btn == "yes") {
            var strUrl = strPath + 'GGFW/DocumentsManage/ClientCommand.fwf?Method=DelClientByID';
            Ext.Ajax.request({
                url: strUrl,
                method: 'POST',
                params: {
                    client_ids: client_ids
                },
                failure: function(response, options) {
                    ReturnValue = Ext.MessageBox.alert("警告", "网络异常或登录过期，请重新登录！");
                },
                success: function(response, options) {
                    var json = response.responseText;
                    var obj = parseJsonResult(json);
                    if (!obj.Success) {
                        showMessage("错误！", "删除数据出现错误！请查看详细原因。", obj.Message);
                    }
                    else {
                        // 删除成功后刷新页面
                        doSearchClientList(false);
                    }
                }
            });
        }
    });
}